package cheme.ProcessEditor.Figures.Util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.AbstractLocator;
import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.ConnectionLocator;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Polyline;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;

import cheme.ProcessEditor.Figures.Util.PositionCalculator.Position;

public class CalloutLineLocator extends ConnectionLocator
{
	ChopboxAnchor anchor;
	
	IFigure figure;
	public CalloutLineLocator(Connection connection, IFigure figure)
	{
		super(connection);
		
		anchor = new ChopboxAnchor(figure);
		
		this.figure = figure;
	}
	
	protected Point nearest(Point figureCenter, Connection connection)
	{
		Point result = null;
		
		List distances = new ArrayList();
		
		Point first = connection.getPoints().getFirstPoint();
		distances.add(distance(figureCenter, first));
	
		Point mid = connection.getPoints().getMidpoint();
		distances.add(distance(figureCenter, mid));
		
		Point last = connection.getPoints().getLastPoint();
		distances.add(distance(figureCenter, last));
		
		Object min = Collections.min(distances);
		
		switch (distances.indexOf(min))
		{
		case 0:
			result = first;
			break;
		case 1:
			result = mid;
			break;
		case 2:
			result = last;
			break;
		default:
			break;
		}
		
		return result;
	}
	
	double distance(Point first, Point second)
	{
		return Math.sqrt(Math.pow(first.x - second.x, 2) + Math.pow(first.y - second.y, 2));
	}
	
	@Override
	public void relocate(IFigure target)
	{
		if(target instanceof Polyline)
		{
			Polyline line = (Polyline)target;
		
//			Point figureCenter = PositionCalculator.getPoint(callout.getBounds(), Position.Center);
			
			Point connectionIntersection = getConnection().getPoints().getMidpoint();//nearest(figureCenter, getConnection());
			Point figureIntersection = anchor.getLocation(connectionIntersection);
			
			line.setStart(connectionIntersection);
			line.setEnd(figureIntersection);
			
			if(figure.getBounds().contains(connectionIntersection))
			{
				line.setVisible(false);
			}
			else
			{
				line.setVisible(true);
			}

		}
	}
	
	
	Point getStartPoint(PointList points)
	{
		return points.getFirstPoint();
	}
	
	Point getEndPoint(PointList points)
	{
		return points.getLastPoint();
	}
	
	Point getMiddlePoint(PointList points)
	{
		if (points.size() % 2 == 0) {
			int i = points.size() / 2;
			int j = i - 1;
			Point p1 = points.getPoint(j);
			Point p2 = points.getPoint(i);
			Dimension d = p2.getDifference(p1);
			return Point.SINGLETON.setLocation(p1.x + d.width / 2, p1.y + d.height / 2);
		}
		int i = (points.size() - 1) / 2;
		return points.getPoint(Point.SINGLETON, i);
	}
}
